package view;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import controller.Controller;

import model.ModelReader;

public class CommandPanel extends GamePanel {
	private static final long serialVersionUID = 1L;
	private JButton[] buttons;
	private ModelReader mr;
	private ButtonState buttonState;
	private ActionListener moveButton, workButton, haltButton, placeItemButton, craftButton;

	public CommandPanel(Stage parent, Controller controller, int w, int h){
		super(parent, controller, w, h);
		this.mr = parent.getModelReader();
	}

	protected void init() {
		moveButton = new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				controller.clickMove();
			}
		};
		workButton = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				controller.clickWork();
			}
		};
		haltButton = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				controller.clickHalt();
			}
		};
		placeItemButton = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				controller.clickPlaceItem();
			}
		};
		craftButton = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				controller.clickCraftTable();
			}
		};
		buttonState = ButtonState.NULL;
		setBackground(Color.lightGray);
		GridLayout gl = new GridLayout(3, 3);
		gl.setHgap(2);
		gl.setVgap(2);
		this.setLayout(gl);
		buttons = new JButton[9];

		for(int i = 0; i < 9; i++){
			buttons[i] = new JButton();
			buttons[i].setVisible(false);
			add(buttons[i]);
		}
	}

	@Override
	protected void update() {
		boolean stateChanged = false;
		if(buttonState != ButtonState.PLAYER && mr.entitySelected()){
			if(mr.getSelectedEntityID() == 1){
				playerButtons();
				stateChanged = true;
			}
			buttonState = ButtonState.PLAYER;
		}
		if (buttonState != ButtonState.NULL && !mr.entitySelected()){
			for(int i = 0; i < 9; i++){
				buttons[i].setVisible(false);
			}
			buttonState = ButtonState.NULL;
			stateChanged = true;
		}
		if(buttonState == ButtonState.PLAYER){
			if(controller.isCraftingOpen()){
				buttons[8].setName("Close Crafting Table");
				buttons[8].setIcon(new ImageIcon(getClass().getResource("/resource/Commands/CraftStop.png")));
				stateChanged = true;
			}else if(!controller.isCraftingOpen()){
				buttons[8].setName("Open Craft Table");
				buttons[8].setIcon(new ImageIcon(getClass().getResource("/resource/Commands/Craft.png")));
				stateChanged = true;
			}
			if(mr.placeableItemSelected() && !buttons[2].getName().equals("Place Item")){
				buttons[2].addActionListener(placeItemButton);
				buttons[2].setName("Place Item");
				buttons[2].setIcon(new ImageIcon(getClass().getResource("/resource/Commands/Build.png")));
				buttons[2].setVisible(true);
				stateChanged = true;
			}else if(!mr.placeableItemSelected() && buttons[2].getName().equals("Place Item")){
				buttons[2].removeActionListener(placeItemButton);
				buttons[2].setName("");
				buttons[2].setVisible(false);
				stateChanged = true;
			}
			if(mr.entityIsWorking() && buttons[1].getName().equals("Work")){
				buttons[1].removeActionListener(workButton);
				buttons[1].addActionListener(haltButton);
				buttons[1].setName("Halt");
				buttons[1].setIcon(new ImageIcon(getClass().getResource("/resource/Commands/WorkStop.png")));
				stateChanged = true;
			}else if(!mr.entityIsWorking() && buttons[1].getName().equals("Halt")){
				buttons[1].removeActionListener(haltButton);
				buttons[1].addActionListener(workButton);
				buttons[1].setName("Work");
				buttons[1].setIcon(new ImageIcon(getClass().getResource("/resource/Commands/Work.png")));
				stateChanged = true;
			}
		}
		if(stateChanged)
			repaint();
	}

	private void playerButtons() {
		buttons[0].setName("Move");
		buttons[0].setIcon(new ImageIcon(getClass().getResource("/resource/Commands/Move.png")));
		buttons[0].setVisible(true);
		buttons[0].addActionListener(moveButton);

		buttons[1].setName("Work");
		buttons[1].setIcon(new ImageIcon(getClass().getResource("/resource/Commands/Work.png")));
		buttons[1].setVisible(true);
		buttons[1].addActionListener(workButton);
		
		buttons[2].setName("");
		
		buttons[8].setVisible(true);
		buttons[8].addActionListener(craftButton);
	}

	protected enum ButtonState{
		NULL,
		PLAYER,
		PLAYER_PLACEABLE,
		BUILDING;
	}
}
